package com.reform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reform.config.JwtTokenUtil;
import com.reform.pojo.admin.Admin;
import com.reform.pojo.common.Dealt;
import com.reform.pojo.phase.ViewPhaseCheck;
import com.reform.pojo.project.ProjectMember;
import com.reform.pojo.project.SimplifyProjectInfo;
import com.reform.pojo.project.ViewProjectMember;
import com.reform.mapper.ViewProjectMemberMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IAdminService;
import com.reform.service.IExpertService;
import com.reform.service.INumProjectService;
import com.reform.service.IViewProjectMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-10-18
 */
@Service
public class ViewProjectMemberServiceImpl extends ServiceImpl<ViewProjectMemberMapper, ViewProjectMember> implements IViewProjectMemberService {

    @Autowired
    ViewProjectMemberMapper viewProjectMemberMapper;
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    IAdminService adminService;
    @Autowired
    IExpertService expertService;
    @Autowired
    INumProjectService numProjectService;

    @Override
    public Result getAllTemporaryProject() {
        return Result.success(ResultCode.SUCCESS, viewProjectMemberMapper.selectList(null));
    }

    @Override
    public Result getNotTemporaryProject(HttpServletRequest request) throws SQLException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.failure(ResultCode.USER_AUTH_Login_LAPSE);
        }
        String gh = jwtTokenUtil.getGHFromToken(token);
        Integer type = jwtTokenUtil.getTypeFromToken(token);
        QueryWrapper<ViewProjectMember> qw = new QueryWrapper<>();
        qw.ne("pro_approval", 0);

        //专家导航权限为2
        if (type == 2) {
            //获取专家下的批次
            List<String> batchIdList = expertService.getBatchIdByGH(gh, 1);
            return Result.success(ResultCode.SUCCESS, selectBatchIds(batchIdList));
        }
        //管理员权限分为查询与操作管理员
        if (type == 3) {
            //基本院查询管理员为3
            Admin admin = adminService.getAdminByGh(gh);
            //查询管理员此处为0，操作管理员此处为1
            Integer depId = admin.getDepId();
            //校查询管理员为5
            if (depId == 0) {
                //校管理员
                return Result.success(ResultCode.SUCCESS, viewProjectMemberMapper.selectList(qw));
            }
            if (depId != 0) {
                //院管理员
                //看看该教师可以管理的批次
                List<String> batchIdList = numProjectService.getBatchIdByDepId(depId);
                List<ViewProjectMember> viewProjectMembers = selectBatchIds(batchIdList, depId);
                return Result.success(ResultCode.SUCCESS, viewProjectMembers);
            }
        }

        return Result.failure(ResultCode.FAILURE);
    }

    public List<ViewProjectMember> selectBatchIds(List<String> batchIdList, Integer proDepId) {
        List<ViewProjectMember> resList = new ArrayList<>();
        for (String s : batchIdList) {
            resList.addAll(viewProjectMemberMapper.selectList(new QueryWrapper<ViewProjectMember>().eq("batch_id", s).eq("pro_dep_id", proDepId)));
        }
        return resList;
    }

    public List<ViewProjectMember> selectBatchIds(List<String> batchIdList) {
        List<ViewProjectMember> resList = new ArrayList<>();
        for (String s : batchIdList) {
            resList.addAll(viewProjectMemberMapper.selectList(new QueryWrapper<ViewProjectMember>().eq("batch_id", s)));
        }
        return resList;
    }

    @Override
    public List<SimplifyProjectInfo> getSimplifyProjectByPhaseId(String phaseId) {
        return viewProjectMemberMapper.getSimplifyProjectByPhaseId(phaseId);
    }

}
